<template>
    <div class="home-container">
        <AppHeader :user="user" :handleLogout="handleLogout" />

        <div class="secondary-nav-bar">
            <div class="secondary-nav-content">
                <div class="tabs">
                    <button class="tab-button active">全部</button>
                    <button class="tab-button">文章</button>
                    <button class="tab-button active">问答</button>
                </div>
                <button class="publish-button-secondary" @click="router.push('/publish')">发帖</button>
            </div>
        </div>

        <main class="main-content">
            <div class="sidebar">
                <div class="filter-buttons">
                    <button class="filter-button active">最新提问</button>
                    <button class="filter-button">热门问题</button>
                    <button class="filter-button">热门话题</button>
                </div>
            </div>

            <div class="questions-section">
                <div class="questions-list">
                    <div v-for="question in questions" :key="question.id" class="question-card"
                        @click="goDetail(question.id)">
                        <h3>{{ question.title }}</h3>
                        <p>{{ question.summary }}</p>
                        <div class="question-meta">
                            <span>作者: {{ question.author }}</span>
                            <span>发布时间: {{ formatDate(question.createdAt) }}</span>
                            <span>回答数: {{ question.answerCount }}</span>
                            <span>阅读数: {{ question.viewCount }}</span>
                        </div>
                    </div>
                </div>
            </div>
        </main>
    </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import axios from 'axios'
import AppHeader from '../components/AppHeader.vue' // 导入AppHeader组件

const router = useRouter()
const user = ref(JSON.parse(localStorage.getItem('user') || 'null'))
const questions = ref([])

const handleLogout = () => {
    localStorage.removeItem('user')
    router.push('/login')
}

const fetchQuestions = async () => {
    try {
        const response = await axios.get('/api/questions')
        questions.value = response.data
    } catch (error) {
        console.error('获取问题列表失败:', error)
    }
}

const formatDate = (dateString) => {
    return new Date(dateString).toLocaleString()
}

const goDetail = (id) => {
    router.push(`/question/${id}`)
}

onMounted(() => {
    fetchQuestions()
})
</script>

<style scoped>
.home-container {
    min-height: 100vh;
    background-color: #f5f5f5;
}

.secondary-nav-bar {
    background-color: #ffffff;
    border-bottom: 1px solid #e8e8e8;
    box-shadow: 0 1px 6px rgba(0, 0, 0, 0.03);
    padding: 0.7rem 0;
    margin-bottom: 1.5rem;
}

.secondary-nav-content {
    padding: 0 2rem;
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.tabs {
    display: flex;
    gap: 2.2rem;
}

.tab-button {
    background: none;
    border: none;
    padding: 0.4rem 0.6rem;
    font-size: 1rem;
    cursor: pointer;
    color: #666;
    font-weight: 500;
    position: relative;
    transition: color 0.2s ease;
}

.tab-button:hover {
    color: #07c160;
}

.tab-button.active {
    color: #07c160;
    font-weight: 600;
}

.tab-button.active::after {
    content: '';
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    bottom: -3px;
    width: 30%;
    height: 1.5px;
    background-color: #07c160;
    border-radius: 1px;
    transition: width 0.3s ease;
}

.publish-button-secondary {
    padding: 0.4rem 1.2rem;
    background-color: #07c160;
    color: white;
    border: none;
    border-radius: 20px;
    font-size: 0.85rem;
    cursor: pointer;
    font-weight: bold;
    box-shadow: 0 3px 8px rgba(7, 193, 96, 0.25);
    transition: all 0.2s ease;
}

.publish-button-secondary:hover {
    background-color: #05ae56;
    box-shadow: 0 4px 12px rgba(7, 193, 96, 0.35);
}

.main-content {
    padding: 0 2rem;
    width: 100%;
    display: flex;
    gap: 1.8rem;
}

.sidebar {
    flex: 0 0 220px;
    background: white;
    padding: 1.2rem;
    border-radius: 8px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.04);
}

.filter-buttons {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.filter-button {
    background: none;
    border: none;
    padding: 0.6rem 0.8rem;
    text-align: left;
    cursor: pointer;
    font-size: 0.9rem;
    color: #555;
    border-radius: 5px;
    transition: background-color 0.2s ease, color 0.2s ease, box-shadow 0.2s ease;
}

.filter-button:hover {
    background-color: #f2f2f2;
}

.filter-button.active {
    background-color: #07c160;
    color: white;
    font-weight: bold;
    box-shadow: 0 3px 8px rgba(7, 193, 96, 0.15);
}

.questions-section {
    flex: 1;
}

.questions-list {
    display: grid;
    gap: 1rem;
}

.question-card {
    background: white;
    padding: 1.3rem 1.6rem;
    border-radius: 10px;
    box-shadow: 0 3px 15px rgba(0, 0, 0, 0.07);
    cursor: pointer;
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.question-card:hover {
    transform: translateY(-3px);
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.12);
}

.question-card h3 {
    font-size: 1.15rem;
    margin-top: 0;
    margin-bottom: 0.4rem;
    color: #333;
}

.question-card p {
    font-size: 0.85rem;
    color: #666;
    line-height: 1.4;
    margin-bottom: 0.8rem;
}

.question-meta {
    margin-top: 0.5rem;
    display: flex;
    justify-content: space-between;
    color: #888;
    font-size: 0.75rem;
    flex-wrap: wrap;
}

.question-meta span {
    margin-right: 0.8rem;
}

.question-meta span:last-child {
    margin-right: 0;
}
</style>